/* 1videoConference -- An open source video conferencing platform.
* Copyright (C) 2007 - 2008, Adiance Technologies Pvt. Ltd.
*
* Hardik Sanghvi <hardik.sanghvi @adiance.com>
*
* See http://www.1videoconference.org for more information about
* the 1videoConference project. Please do not directly contact
* any of the maintainers of this project for assistance;
* the project provides a web site, forums and mailing lists
* for your use.
*
* This program is free software, distributed under the terms of
* the GNU General Public License Version 2. See the LICENSE file
* at the top of the source tree.
********************************************************************************/

using System;
using System.Collections.Generic;
using System.Text;
using System.ServiceModel;
using System.IO;
//using System.Windows;

using Adiance.VMukti.ZipUnzip;

namespace Adiance.VMukti.BAL
{
    public class ZipUnzip
    {
        public void uploadfile(string filepath, string modulename)
        {
            try
            {
                //if (!File.Exists(filepath))
                //{
                //    MessageBox.Show("Specify the file to upload");
                //    return;
                //}
                string[] FileName = filepath.Split('\\');
                string[] zipname = FileName[FileName.Length - 1].ToString().Split('.');

                System.Reflection.Assembly ass = System.Reflection.Assembly.GetAssembly(typeof(ZipUnzip));

                //string filezip = ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Zip");
                //string filesource = filezip + "\\" + FileName[FileName.Length - 1].ToString();


                if (!Directory.Exists(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Temp")))
                {
                    Directory.CreateDirectory(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Temp"));
                }
                string filetemp = ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Temp");

                if (!Directory.Exists(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Presentation Layer")))
                {
                    Directory.CreateDirectory(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Presentation Layer"));
                }
                string pathview = ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Presentation Layer");



                if (!Directory.Exists(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Data Layer")))
                {
                    Directory.CreateDirectory(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Data Layer"));
                }
                string pathdal = ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Data Layer");



                if (!Directory.Exists(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Business Layer")))
                {
                    Directory.CreateDirectory(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Business Layer"));
                }
                string pathbal = ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Business Layer");



                if (!Directory.Exists(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Image")))
                {
                    Directory.CreateDirectory(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Image"));
                }
                string pathimage = ass.Location.Replace("Adiance.VMukti.BAL.dll", @"Image");



                if (!Directory.Exists(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"SQL Scripts")))
                {
                    Directory.CreateDirectory(ass.Location.Replace("Adiance.VMukti.BAL.dll", @"SQL Scripts"));
                }
                string pathsql = ass.Location.Replace("Adiance.VMukti.BAL.dll", @"SQL Scripts");

                //if (File.Exists(filesource))
                //{
                //    MessageBox.Show("Specify other name of zip file");
                //}
                //else
                //{
                //    if (Directory.Exists(pathbal + "\\" + modulename))
                //    {
                //        MessageBox.Show("This Module name already exists in the database.Specify other name");
                //    }
                //    else
                //    {
                //        File.Copy(filepath, filesource);

                Adiance.VMukti.ZipUnzip.Zip.FastZip fz = new Adiance.VMukti.ZipUnzip.Zip.FastZip();
                fz.ExtractZip(filepath, filetemp, null);
                Directory.CreateDirectory(pathview + "\\" + modulename);
                Directory.CreateDirectory(pathdal + "\\" + modulename);
                Directory.CreateDirectory(pathbal + "\\" + modulename);
                Directory.CreateDirectory(pathsql + "\\" + modulename);


                string[] dirnameorg = Directory.GetDirectories(filetemp);
                string[] dirname = Directory.GetDirectories(dirnameorg[0]);

                ///copyfiles
                for (int i = 0; i < dirname.Length; i++)
                {
                    string[] strSplit = dirname[i].ToString().Split('\\');

                    if (strSplit[strSplit.Length - 1].ToUpper() == "BAL")
                    {
                        string[] balfilenames = Directory.GetFiles(dirname[i].ToString());
                        for (int j = 0; j < balfilenames.Length; j++)
                        {
                            string[] filefoldername = balfilenames[j].Split('\\');
                            File.Copy(balfilenames[j], pathbal + "\\" + modulename + "\\" + filefoldername[filefoldername.Length - 1].ToString());
                        }
                    }

                    if (strSplit[strSplit.Length - 1].ToUpper() == "DAL")
                    {
                        string[] balfilenames = Directory.GetFiles(dirname[i].ToString());
                        for (int j = 0; j < balfilenames.Length; j++)
                        {
                            string[] filefoldername = balfilenames[j].Split('\\');
                            File.Copy(balfilenames[j], pathdal + "\\" + modulename + "\\" + filefoldername[filefoldername.Length - 1].ToString());
                        }

                    }

                    if (strSplit[strSplit.Length - 1].ToUpper() == "CONTROL")
                    {
                        string[] balfilenames = Directory.GetFiles(dirname[i].ToString());
                        for (int j = 0; j < balfilenames.Length; j++)
                        {
                            string[] filefoldername = balfilenames[j].Split('\\');
                            File.Copy(balfilenames[j], pathview + "\\" + modulename + "\\" + filefoldername[filefoldername.Length - 1].ToString());
                        }

                    }

                    if (strSplit[strSplit.Length - 1].ToUpper() == "IMAGES")
                    {
                        string[] balfilenames = Directory.GetFiles(dirname[i].ToString());
                        for (int j = 0; j < balfilenames.Length; j++)
                        {
                            string[] filefoldername = balfilenames[j].Split('\\');
                            File.Copy(balfilenames[j], pathimage + "\\" + modulename + "\\" + filefoldername[filefoldername.Length - 1].ToString());
                        }

                    }

                    if (strSplit[strSplit.Length - 1].ToUpper() == "SQL")
                    {
                        string[] baldirnames = Directory.GetDirectories(dirname[i].ToString());
                        for (int dircount = 0; dircount < baldirnames.Length; dircount++)
                        {
                            string[] files = baldirnames[dircount].Split('\\');
                            Directory.CreateDirectory(pathsql + "\\" + modulename + "\\" + files[files.Length - 1].ToString());
                            string[] sqlfilenames = Directory.GetFiles(baldirnames[dircount]);
                            for (int j = 0; j < sqlfilenames.Length; j++)
                            {
                                string[] filefoldername = sqlfilenames[j].Split('\\');
                                File.Copy(sqlfilenames[j], pathsql + "\\" + modulename + "\\" + files[files.Length - 1].ToString() + "\\" + filefoldername[filefoldername.Length - 1].ToString());
                            }
                        }
                    }

                }
                Directory.Delete(dirnameorg[0], true);
                //string[] dir = Directory.GetDirectories(filetemp);
                //for (int k = 0; k < dir.Length - 1; k++)
                //{
                //    Directory.Delete(dir[k], true);
                //}
            }
            catch (Exception exp)
            {
                exp.Data.Add("My Key", "Adiance.VMukti.VCServer--:--ZipUnzip.cs--:--uploadfile()--:--");
                Adiance.VMukti.BAL.clsUtility.LogException = exp;
                //System.Windows.MessageBox.Show("uploadfile=" + exp.Message);
            }
        }
    }     
}
